-- Copyright (c) 2024 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at:
--          http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.
local validate = require 'mc.validate'
local utils = require 'mc.utils'
local mdb = require 'mc.mdb'

local SPDMMeasurement = {}

---@class SPDMMeasurement.SecurityVersionNumber
---@field SecurityVersionNumber string
local TSecurityVersionNumber = {}
TSecurityVersionNumber.__index = TSecurityVersionNumber
TSecurityVersionNumber.group = {}

local function TSecurityVersionNumber_from_obj(obj)
    return setmetatable(obj, TSecurityVersionNumber)
end

function TSecurityVersionNumber.new(SecurityVersionNumber)
    return TSecurityVersionNumber_from_obj({SecurityVersionNumber = SecurityVersionNumber})
end
---@param obj SPDMMeasurement.SecurityVersionNumber
function TSecurityVersionNumber:init_from_obj(obj)
    self.SecurityVersionNumber = obj.SecurityVersionNumber
end

function TSecurityVersionNumber:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TSecurityVersionNumber.group)
end

TSecurityVersionNumber.from_obj = TSecurityVersionNumber_from_obj

TSecurityVersionNumber.proto_property = {'SecurityVersionNumber'}

TSecurityVersionNumber.default = {''}

TSecurityVersionNumber.struct = {{name = 'SecurityVersionNumber', is_array = false, struct = nil}}

function TSecurityVersionNumber:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'SecurityVersionNumber', self.SecurityVersionNumber, 'string', true, errs, need_convert)

    TSecurityVersionNumber:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TSecurityVersionNumber.proto_property, errs, need_convert)
    return self
end

function TSecurityVersionNumber:unpack(_)
    return self.SecurityVersionNumber
end

SPDMMeasurement.SecurityVersionNumber = TSecurityVersionNumber

---@class SPDMMeasurement.PartofSummaryHash
---@field PartofSummaryHash boolean
local TPartofSummaryHash = {}
TPartofSummaryHash.__index = TPartofSummaryHash
TPartofSummaryHash.group = {}

local function TPartofSummaryHash_from_obj(obj)
    return setmetatable(obj, TPartofSummaryHash)
end

function TPartofSummaryHash.new(PartofSummaryHash)
    return TPartofSummaryHash_from_obj({PartofSummaryHash = PartofSummaryHash})
end
---@param obj SPDMMeasurement.PartofSummaryHash
function TPartofSummaryHash:init_from_obj(obj)
    self.PartofSummaryHash = obj.PartofSummaryHash
end

function TPartofSummaryHash:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TPartofSummaryHash.group)
end

TPartofSummaryHash.from_obj = TPartofSummaryHash_from_obj

TPartofSummaryHash.proto_property = {'PartofSummaryHash'}

TPartofSummaryHash.default = {false}

TPartofSummaryHash.struct = {{name = 'PartofSummaryHash', is_array = false, struct = nil}}

function TPartofSummaryHash:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'PartofSummaryHash', self.PartofSummaryHash, 'bool', true, errs, need_convert)

    TPartofSummaryHash:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TPartofSummaryHash.proto_property, errs, need_convert)
    return self
end

function TPartofSummaryHash:unpack(_)
    return self.PartofSummaryHash
end

SPDMMeasurement.PartofSummaryHash = TPartofSummaryHash

---@class SPDMMeasurement.LastUpdated
---@field LastUpdated integer
local TLastUpdated = {}
TLastUpdated.__index = TLastUpdated
TLastUpdated.group = {}

local function TLastUpdated_from_obj(obj)
    return setmetatable(obj, TLastUpdated)
end

function TLastUpdated.new(LastUpdated)
    return TLastUpdated_from_obj({LastUpdated = LastUpdated})
end
---@param obj SPDMMeasurement.LastUpdated
function TLastUpdated:init_from_obj(obj)
    self.LastUpdated = obj.LastUpdated
end

function TLastUpdated:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TLastUpdated.group)
end

TLastUpdated.from_obj = TLastUpdated_from_obj

TLastUpdated.proto_property = {'LastUpdated'}

TLastUpdated.default = {0}

TLastUpdated.struct = {{name = 'LastUpdated', is_array = false, struct = nil}}

function TLastUpdated:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'LastUpdated', self.LastUpdated, 'uint64', true, errs, need_convert)

    TLastUpdated:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TLastUpdated.proto_property, errs, need_convert)
    return self
end

function TLastUpdated:unpack(_)
    return self.LastUpdated
end

SPDMMeasurement.LastUpdated = TLastUpdated

---@class SPDMMeasurement.Measurement
---@field Measurement string
local TMeasurement = {}
TMeasurement.__index = TMeasurement
TMeasurement.group = {}

local function TMeasurement_from_obj(obj)
    return setmetatable(obj, TMeasurement)
end

function TMeasurement.new(Measurement)
    return TMeasurement_from_obj({Measurement = Measurement})
end
---@param obj SPDMMeasurement.Measurement
function TMeasurement:init_from_obj(obj)
    self.Measurement = obj.Measurement
end

function TMeasurement:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TMeasurement.group)
end

TMeasurement.from_obj = TMeasurement_from_obj

TMeasurement.proto_property = {'Measurement'}

TMeasurement.default = {''}

TMeasurement.struct = {{name = 'Measurement', is_array = false, struct = nil}}

function TMeasurement:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'Measurement', self.Measurement, 'string', true, errs, need_convert)

    TMeasurement:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TMeasurement.proto_property, errs, need_convert)
    return self
end

function TMeasurement:unpack(_)
    return self.Measurement
end

SPDMMeasurement.Measurement = TMeasurement

---@class SPDMMeasurement.MeasurementType
---@field MeasurementType string
local TMeasurementType = {}
TMeasurementType.__index = TMeasurementType
TMeasurementType.group = {}

local function TMeasurementType_from_obj(obj)
    return setmetatable(obj, TMeasurementType)
end

function TMeasurementType.new(MeasurementType)
    return TMeasurementType_from_obj({MeasurementType = MeasurementType})
end
---@param obj SPDMMeasurement.MeasurementType
function TMeasurementType:init_from_obj(obj)
    self.MeasurementType = obj.MeasurementType
end

function TMeasurementType:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TMeasurementType.group)
end

TMeasurementType.from_obj = TMeasurementType_from_obj

TMeasurementType.proto_property = {'MeasurementType'}

TMeasurementType.default = {''}

TMeasurementType.struct = {{name = 'MeasurementType', is_array = false, struct = nil}}

function TMeasurementType:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'MeasurementType', self.MeasurementType, 'string', true, errs, need_convert)

    if self.MeasurementType ~= nil then
        validate.Enum(prefix .. 'MeasurementType', self.MeasurementType, '', {
            'FirmwareConfiguration', 'HardwareConfiguration', 'ImmutableROM', 'MeasurementManifest', 'MutableFirmware',
            'MutableFirmwareSecurityVersionNumber', 'MutableFirmwareVersion'
        }, errs, need_convert)
    end

    TMeasurementType:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TMeasurementType.proto_property, errs, need_convert)
    return self
end

function TMeasurementType:unpack(_)
    return self.MeasurementType
end

SPDMMeasurement.MeasurementType = TMeasurementType

---@class SPDMMeasurement.HashAlgorithm
---@field HashAlgorithm string
local THashAlgorithm = {}
THashAlgorithm.__index = THashAlgorithm
THashAlgorithm.group = {}

local function THashAlgorithm_from_obj(obj)
    return setmetatable(obj, THashAlgorithm)
end

function THashAlgorithm.new(HashAlgorithm)
    return THashAlgorithm_from_obj({HashAlgorithm = HashAlgorithm})
end
---@param obj SPDMMeasurement.HashAlgorithm
function THashAlgorithm:init_from_obj(obj)
    self.HashAlgorithm = obj.HashAlgorithm
end

function THashAlgorithm:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, THashAlgorithm.group)
end

THashAlgorithm.from_obj = THashAlgorithm_from_obj

THashAlgorithm.proto_property = {'HashAlgorithm'}

THashAlgorithm.default = {''}

THashAlgorithm.struct = {{name = 'HashAlgorithm', is_array = false, struct = nil}}

function THashAlgorithm:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'HashAlgorithm', self.HashAlgorithm, 'string', true, errs, need_convert)

    THashAlgorithm:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, THashAlgorithm.proto_property, errs, need_convert)
    return self
end

function THashAlgorithm:unpack(_)
    return self.HashAlgorithm
end

SPDMMeasurement.HashAlgorithm = THashAlgorithm

---@class SPDMMeasurement.Index
---@field Index integer
local TIndex = {}
TIndex.__index = TIndex
TIndex.group = {}

local function TIndex_from_obj(obj)
    return setmetatable(obj, TIndex)
end

function TIndex.new(Index)
    return TIndex_from_obj({Index = Index})
end
---@param obj SPDMMeasurement.Index
function TIndex:init_from_obj(obj)
    self.Index = obj.Index
end

function TIndex:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TIndex.group)
end

TIndex.from_obj = TIndex_from_obj

TIndex.proto_property = {'Index'}

TIndex.default = {0}

TIndex.struct = {{name = 'Index', is_array = false, struct = nil}}

function TIndex:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'Index', self.Index, 'uint8', true, errs, need_convert)

    TIndex:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TIndex.proto_property, errs, need_convert)
    return self
end

function TIndex:unpack(_)
    return self.Index
end

SPDMMeasurement.Index = TIndex

SPDMMeasurement.interface = mdb.register_interface('bmc.kepler.RoT.ComponentIntegrity.SPDMMeasurement', {
    Index = {'y', {}, true, nil, false},
    HashAlgorithm = {'s', {}, true, nil, false},
    MeasurementType = {'s', {}, true, nil, false},
    Measurement = {'s', {}, true, nil, false},
    LastUpdated = {'t', {}, true, nil, false},
    PartofSummaryHash = {'b', {}, true, nil, false},
    SecurityVersionNumber = {'s', {}, true, nil, false}
}, {}, {})

return SPDMMeasurement
